package com.infocollect.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.infocollect.common.utils.http.HttpUtils;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;


public class BankInfoUtils {
	public static void main(String[] args) {
		String result = bankVerifyNew("贾玲芝","603367100186742173","","");
		System.out.println(result);
	}
	public static String bankVerifyNew(String acct_name,String acct_pan,String cert_id,String cert_type){
		String host = "http://lundroid.market.alicloudapi.com/lianzhuo/verifi";
		String method = "GET";
		String appcode = "1448f3ec4bff4714bdcdab97b1b0e6b7";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE " + appcode);
		Map<String, Object> querys = new HashMap<String,Object>();
		querys.put("acct_name", acct_name);
		querys.put("acct_pan", acct_pan);
		querys.put("cert_id", cert_id);
		String result = HttpClientUtils.sendGet(host, headers, querys);
		return result;
	}
	/**
	 * 银行卡三元素校验
	 * @param acct_name
	 * @param acct_pan
	 * @param cert_id
	 * @param cert_type
	 * @return
	 */
	public static String bankVerify(String acct_name,String acct_pan,String cert_id,String cert_type) {
//		String host = "https://ali-bankcard3.showapi.com/bank3";
		String host = "https://bankcard1.market.alicloudapi.com/bank3";
	    String method = "GET";
	    String appcode = "d1ac02cca19f4d54ba806c098d76e685";
//	    String appcode = GlobalUtil.getValue("bankAppCode");
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, Object> querys = new HashMap<String,Object>();
	    querys.put("acct_name", acct_name);
	    querys.put("acct_pan", acct_pan);
	    querys.put("cert_id", cert_id);
	    querys.put("cert_type",cert_type);
	    querys.put("needBelongArea", "true");
	    String result = HttpClientUtils.sendGet(host, headers, querys);

		return result;
	}
	public static Map<String,String> checkBankCard(String acct_name,String acct_pan,String cert_id,String cert_type) {
		Map<String,String> map = new HashMap<String,String>();
		String result = bankVerify(acct_name,acct_pan,cert_id,cert_type);
		if (StringUtils.isEmpty(result)) {
            map.put("code", "-1");
            map.put("msg", "银行卡校验接口异常");
            return map;
        }
		JSONObject jsonObject = JSONObject.parseObject(result);
        String resCode = jsonObject.getString("showapi_res_code");
        String resError = jsonObject.getString("showapi_res_error");
        if (!"0".equals(resCode)) {
        	 map.put("code", "-1");
             map.put("msg", "银行卡校验失败:"+resError);
             return map;
        }
        JSONObject obj = jsonObject.getJSONObject("showapi_res_body");
        String code = obj.getString("code");
        String msg = obj.getString("msg");
        if (!"0".equals(code)) {
       	 	map.put("code", "-1");
            map.put("msg", "银行卡校验失败:"+resError);
            return map;
       }
        JSONObject bankObj = obj.getJSONObject("belong");
        String bankName = bankObj.getString("bankName");
        String bankType = bankObj.getString("cardType");
        map.put("bankName", bankName);
        map.put("cardType", bankType);
        map.put("code", "0");
		return map;
	}
}
